import 'package:flutter/material.dart';
import 'package:ileddemo/code/ui/base/BaseWidget.dart';
import 'package:ileddemo/code/ui/base/TitleBarWidget.dart';
import 'package:lib_bt/BleManager.dart';
import 'package:lib_bt/send/BleSendManager.dart';
import 'package:lib_bt/send/PackageInterface.dart';
import 'package:lib_bt/send/SendBean.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../../../generated/l10n.dart';
import '../../ble/protocol/iledcolor/send/ColorPackageImpl.dart';
import '../language/LanguagePage.dart';

//设置
class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  //亮度
  double _brightness = 10;

  //屏幕开关
  bool screenSwitch = true;

  //app版本信息
  String appVersion = "";

  static Future<PackageInfo> getPackageInfo() async {
    return PackageInfo.fromPlatform();
  }

  @override
  void initState() {
    getPackageInfo().then((packageInfo) {
      setState(() {
        appVersion = packageInfo.version;
      });
    });
    super.initState();
  }

  //设置一些简单的数据
  void setAll(PackageInterface package, bool hasResponse) {
    var bleBeans = BleManager.instance.getConnectBleBeans();
    if (bleBeans.isEmpty) {
      return;
    }
    List<SendBean> sendBeans = [];
    for (var bleBean in bleBeans) {
      SendBean sendBean = SendBean()
        ..address = bleBean.address
        ..hasResponse = hasResponse
        ..addPackage(package);
      sendBeans.add(sendBean);
    }
    BleSendManager.instance.send(sendBeans);
  }

  @override
  Widget build(BuildContext context) {
    return BaseWidget(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          TitleBarWidget(title: S.of(context).str_id_21),
          //APP版本
          _Item(title: S.of(context).str_id_22, subTitle: "V $appVersion"),
          //切换语言
          _Item(
            title: S.of(context).str_id_38,
            onTap: () {
              Navigator.of(context).push(MaterialPageRoute(builder: (context) => const LanguagePage()));
            },
          ),
          //清空节目
          _Item(
            title: S.of(context).str_id_36,
            onTap: () => setAll(ColorPackageImpl.createSimplePackage([0x54, 0x02], [0]), false),
          ),
          //亮度设置
          _ItemBg(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(S.of(context).str_id_23, style: TextStyle(fontSize: 16)),
                Slider(
                  min: 1,
                  max: 10,
                  value: _brightness,
                  onChangeEnd: (value) {
                    setAll(ColorPackageImpl.createSimplePackage([0x54, 0x09], [10 - _brightness.toInt(), 0]), false);
                  },
                  onChanged: (value) {
                    setState(() {
                      _brightness = value;
                    });
                  },
                ),
              ],
            ),
          ),
          //屏幕开关
          _ItemBg(
            child: Row(
              children: [
                Text(S.of(context).str_id_24, style: TextStyle(fontSize: 16)),
                Spacer(),
                Switch(
                  value: screenSwitch,
                  onChanged: (value) {
                    setAll(
                      ColorPackageImpl.createSimplePackage([0x54, 0x0A], [value ? 1 : 0, 0, 0, 0, 0, 0, 0, 0, 0]),
                      false,
                    );
                    setState(() {
                      screenSwitch = value;
                    });
                    var package = ColorPackageImpl.createSimplePackage([0x54, 0x09], [10 - _brightness.toInt(), 0]);

                    var bleBeans = BleManager.instance.getConnectBleBeans();
                    if (bleBeans.isEmpty) {
                      return;
                    }
                    List<SendBean> sendBeans = [];
                    for (var bleBean in bleBeans) {
                      SendBean sendBean = SendBean()
                        ..address = bleBean.address
                        ..hasResponse = false
                        ..addPackage(package);
                      sendBeans.add(sendBean);
                    }
                    BleSendManager.instance.send(sendBeans);
                  },
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class _Item extends StatelessWidget {
  final String title;

  //副标题
  final String? subTitle;
  final GestureTapCallback? onTap;

  const _Item({super.key, required this.title, this.subTitle, this.onTap});

  @override
  Widget build(BuildContext context) {
    return _ItemBg(
      onTap: onTap,
      child: Row(
        children: [
          Text(title, style: TextStyle(fontSize: 16)),
          Spacer(),
          Row(
            children: [
              Text(subTitle ?? "", style: TextStyle(fontSize: 16)),
              Icon(Icons.arrow_forward_ios, size: 16),
            ],
          ),
        ],
      ),
    );
  }
}

class _ItemBg extends StatelessWidget {
  final Widget child;
  final GestureTapCallback? onTap;

  const _ItemBg({super.key, required this.child, this.onTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: onTap,
      child: Container(
        margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 0),
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
        decoration: BoxDecoration(color: Colors.blueGrey),
        child: child,
      ),
    );
  }
}
